package com.woniu.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.woniu.entity.FlowerComment;
import com.woniu.entity.FlowerOrder;
import com.woniu.mapper.FlowerCommentMapper;
import com.woniu.mapper.FlowerOrderMapper;
import com.woniu.service.EvaluateService;
import com.woniu.vo.FlowerCommentVoCZT;
import com.woniu.vo.MyPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EvaluateServiceImpl implements EvaluateService {
    @Resource
    private FlowerCommentMapper flowerCommentMapper;

    @Resource
    private FlowerOrderMapper flowerOrderMapper;

    @Resource
    private FlowerOrderMapper fom;

    @Override
    public List<FlowerComment> findAllFlowerComment() {
        return flowerCommentMapper.selectAll();
    }

    @Override
    public List<FlowerComment> findAllFlowerCommentByAccountId(Integer accountId) {
        return flowerCommentMapper.selectAllByAccountId(accountId);
    }

    @Override
    public List<FlowerCommentVoCZT> findAllFlowerCommentVoByAccountId(Integer accountId) {
        return flowerCommentMapper.selectAllFlowerCommentVoByAccountId(accountId);
    }


    /*@Override
    public int addFlowerComment(FlowerComment flowerComment) {
        int i = flowerCommentMapper
                .insertComment(flowerComment.getContent(),
                        flowerComment.getFlowerId(),
                        flowerComment.getAccountId(),
                        flowerComment.getCommentImg(),
                        flowerComment.getRateScore());
        if (i > 0) {
            return flowerComment.getContentId();
        }else {
            return 0;
        }
    }*/

    @Override
    public int addFlowerComment(FlowerComment flowerComment) {
        flowerComment.setIsDelete((byte) 0);
        flowerComment.setCreateTime(new DateTime());
        int i = flowerCommentMapper.insertComment(flowerComment);
        if (i > 0) {
            System.out.println("+++++++++++++++++++++++++++++++++++++" + flowerComment.getCreateTime());
            return flowerComment.getContentId();
        } else {
            return 0;
        }
    }

    @Override
    public List<FlowerCommentVoCZT> findAllFlowerCommentVoByAccountIdAndPAge(MyPage myPage) {
        PageHelper.startPage(myPage.getPageNum(), myPage.getPageSize());
        return flowerCommentMapper.selectAllFlowerCommentVoByAccountId(myPage.getAccountId());
    }

    @Override
    public int deleteFlowerCommentByCommentId(Integer commentId) {
        return flowerCommentMapper.deleteByPrimaryKey(commentId);
    }

    @Override
    public List<FlowerCommentVoCZT> findAllFlowerCommentVoByAccountIdAndPageAndFlowerId(MyPage myPage) {
        PageHelper.startPage(myPage.getPageNum(), myPage.getPageSize());
        return flowerCommentMapper.findAllFlowerCommentVoByAccountIdAndFlowerId(myPage.getAccountId(), myPage.getFlowerId());
    }

    @Override
    public List<FlowerCommentVoCZT> findAllFlowerCommentVoByAccountIdAndFlowerId(MyPage myPage) {
        return flowerCommentMapper.findAllFlowerCommentVoByAccountIdAndFlowerId(myPage.getAccountId(), myPage.getFlowerId());
    }

    @Override
    public boolean changeOrder(Long orderId) {
        int i = flowerCommentMapper.updateOrderIsCommentByOrderId(orderId, 1);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean changeOrderItem(Integer cartId, Integer contentId) {
        int i = flowerCommentMapper.updateOrderItemByCartId(cartId, contentId);
        if (i > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public int getOrderToEval(Long orderId) {
        FlowerOrder order = flowerOrderMapper.selectByPrimaryKey(orderId);
        return order.getIsComment();
    }


}
